package com.lechi.web.listener;

import com.lechi.iot.netty.channel.ITcpChannel;
import com.lechi.iot.netty.listener.TcpListener;
import com.lechi.iot.netty.message.TcpMessage;
import com.lechi.iot.netty.utils.NettyCacheUtils;
import com.lechi.web.constants.TcpMessageConstants;
import com.lechi.web.framework.util.GuavaCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 处理注册接口逻辑
 */
@Slf4j
@Component
public class DeviceRegisterListener implements TcpListener {

    @Override
    public void receive(ITcpChannel channel, TcpMessage message) {
        if (message.adviceAddr() == TcpMessageConstants.DEVICE_REGISTER) {
            String deviceSN = message.deviceSn();
            channel.setDeviceSn(deviceSN);
            String hostName = channel.hostName();
            NettyCacheUtils.put(hostName, channel);
            GuavaCacheUtil.getBiDeviceSnAndIPMap().forcePut(deviceSN, hostName);
            log.debug("监听接收到设备注册地址：" + hostName + "，注册序列号: " + deviceSN);
        }
    }
}
